package ink.xiaobaibai.controller.buy;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ink.xiaobaibai.autoConfig.roleResources.ResourcesRoleMapping;
import ink.xiaobaibai.common.MyRole;
import ink.xiaobaibai.common.ReportType;
import ink.xiaobaibai.entity.AdminUser;
import ink.xiaobaibai.entity.ReportBuyOrder;
import ink.xiaobaibai.response.ResponseFormat;
import ink.xiaobaibai.service.IAdminUserService;
import ink.xiaobaibai.service.IDepartmentService;
import ink.xiaobaibai.service.IReportBuyOrderService;
import ink.xiaobaibai.units.MySecurity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @description: 购买报表集合
 * @author: 小白白
 * @create: 2021-06-15
 **/

@RestController
@RequestMapping("/buy/finance")
@CrossOrigin
@Api(tags = "购买报表接口")
@Slf4j
@ResourcesRoleMapping(resourceName = "购买业务报表", includeRoleNames = {MyRole.ADMIN, MyRole.POWER_REPORT})
public class ReportFinanceBuyController {

    @Autowired
    private IAdminUserService adminUserService;

    @Autowired
    private IReportBuyOrderService reportBuyOrderService;

    @Autowired
    private IDepartmentService departmentService;

    @SuppressWarnings("all")
    @GetMapping("/list-person")
    @ApiOperation("获取个人报表集合")
    public ResponseFormat listPerson(@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
                                     @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize,
                                     @RequestParam(value = "userId", required = false) Integer userId,
                                     @RequestParam(value = "time", required = false) String time) {
        String role = MySecurity.getRole();
        Set<String> roles = MySecurity.getRoles();
        Integer auUserId = MySecurity.getUserId();
        QueryWrapper<ReportBuyOrder> q1 = new QueryWrapper<>();
        if (time != null) {
            q1.eq("time", time);
        }
        q1.orderByDesc("time");
        q1.eq("type", ReportType.PERSONAL.getType());
        if (!roles.contains(MyRole.POWER_REPORT)) {
            if (MyRole.DISTRIBUTOR.equals(role)) {
                //只能获取自己的报表
                String userName = adminUserService.getNameAndPhone(auUserId).getUserName();
                q1.eq("type_id", userName);
                return ResponseFormat.success(reportBuyOrderService.page(new Page<>(pageNum, pageSize), q1));
            }
            if (MyRole.AGENT.equals(role)) {
                //只能获取同部门的报表
                if (userId != null && !adminUserService.identicalDep(userId, auUserId)) {
                    return ResponseFormat.fail("只能查看自己部门人员报表");
                }
                if (userId != null) {
                    String userName = adminUserService.getNameAndPhone(userId).getUserName();
                    q1.eq("type_id", userId);
                } else {
                    //当前部门的人员
                    List<Integer> ids = adminUserService.getUserIdByDepId(adminUserService.getDepId(auUserId));
                    QueryWrapper<AdminUser> q3 = new QueryWrapper<>();
                    q3.select("user_name");
                    q3.in("id", ids);
                    List<String> list = adminUserService.list(q3).stream().map(u -> u.getUserName()).collect(Collectors.toList());
                    q1.in("type_id", list);
                }
                return ResponseFormat.success(reportBuyOrderService.page(new Page<>(pageNum, pageSize), q1));
            }
        }
        if (userId != null) {
            String userName = adminUserService.getNameAndPhone(userId).getUserName();
            q1.eq("type_id", userName);
        }
        return ResponseFormat.success(reportBuyOrderService.page(new Page<>(pageNum, pageSize), q1));
    }

    @SuppressWarnings("all")
    @GetMapping("/list-dep")
    @ApiOperation("获取部门报表集合(depId只能由管理员传入)")
    public ResponseFormat listDep(@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
                                  @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize,
                                  @RequestParam(value = "depId", required = false) Integer depId,
                                  @RequestParam(value = "time", required = false) String time) {
        String role = MySecurity.getRole();
        Set<String> roles = MySecurity.getRoles();
        Integer userId = MySecurity.getUserId();
        QueryWrapper<ReportBuyOrder> q1 = new QueryWrapper<>();
        q1.orderByDesc("time");
        if (depId != null) {
            String departmentName = departmentService.getById(depId).getDepartmentName();
            q1.eq("type_id", departmentName);
        }
        if (time != null) {
            q1.eq("time", time);
        }
        if (!roles.contains(MyRole.POWER_REPORT)) {
            if (role.equals(MyRole.DISTRIBUTOR)) {
                return ResponseFormat.fail("无权限");
            }
            if (role.equals(MyRole.AGENT)) {
                String departmentName = departmentService.getById(adminUserService.getDepId(userId)).getDepartmentName();
                q1.eq("type_id", departmentName);
            }
        }
        return ResponseFormat.success(reportBuyOrderService.page(new Page<>(pageNum, pageSize), q1));
    }

}
